
if(!isFunction(Armor, onMount    )) eval("function Armor::onMount (){}");
if(!isFunction(Armor, onRemove   )) eval("function Armor::onRemove(){}");

package RedBase_SlotDatablock {
	function Armor::onMount(%data, %player, %obj, %slot) {
		parent::onMount(%data, %player, %obj, %slot);
		SlotDatablock_Mount(%player);
	}
	function Armor::doDismount(%data, %player, %forced) {
		parent::doDismount(%data, %player, %forced);
		if(isObject(%player)) {
			SlotDatablock_Dismount(%player);
		}
	}
	function Armor::onRemove(%data, %obj) {
		parent::onRemove(%data, %obj);
		
		for(%i=0; %i<%data.numMountPoints; %i++) {
			%rider = %obj.getMountNodeObject(%i);
			if(isObject(%rider)) {
				SlotDatablock_Dismount(%rider);
			}
		}
	}
	function Player::setDatablock(%player, %data) {
		if(SlotDatablock_ChangeDatablock(%player, %data)) {
			parent::setDatablock(%player, %data);
		}
	}
	function AIPlayer::setDatablock(%player, %data) {
		if(SlotDatablock_ChangeDatablock(%player, %data)) {
			parent::setDatablock(%player, %data);
		}
	}
	
	function serverCmdUseTool(%client, %slot) {
		if(isObject(%client.player) && %client.player.RedSlot_DisableTools) return;
		parent::serverCmdUseTool(%client, %slot);
	}
	function fxDtsBrickData::onUse(%brickdata, %player, %slot) {
		if(isObject(%player) && %player.RedSlot_DisableTools) return;
		parent::onUse(%brickdata, %player, %slot);
	}
};
activatePackage(RedBase_SlotDatablock);

function SlotDatablock_Mount(%player) {
	SlotDatablock_Dismount(%player);
	
	%vehicle = %player.getObjectMount();
	%vdata = %vehicle.getDatablock();
	%slot = %player.getMountNode();
	
	%newdata = %vdata.slotPlayerDatablock[%slot];
	if(%newdata !$= "" && %player.SlotDatablock_OldDatablock $= "") {
		%olddata = %player.getDatablock();
		%player.setDatablock(%newdata);
		%player.SlotDatablock_OldDatablock = %olddata;
	}
	
	if(%vdata.slotDisableTools[%slot]) {
		%player.RedSlot_DisableTools = 1;
		serverCmdUnUseTool(%player.client);
		%player.unmountImage(0);
		%player.playThread(1, root);
	}
}

function SlotDatablock_Dismount(%player) {
	if(%player.SlotDatablock_OldDatablock !$= "") {
		%olddata = %player.SlotDatablock_OldDatablock;
		%player.SlotDatablock_OldDatablock = "";
		%player.setDatablock(%olddata);
	}
	
	%player.RedSlot_DisableTools = 0;
}

function SlotDatablock_ChangeDatablock(%player, %data) {
	if(%player.SlotDatablock_OldDatablock !$= "") {
		%player.SlotDatablock_OldDatablock = %data;
		return 0;
	} else {
		return 1;
	}
}
